package org.swift.mashup.engine.concurrency.execution;

import java.io.Serializable;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExecutionOutputMode implements Serializable {
    private static final Map<String, ExecutionOutputMode> allModes = new HashMap<String, ExecutionOutputMode>();
    private static final Set<String> namespaces = new HashSet<String>();

    public static final ExecutionOutputMode HEADER
            = new ExecutionOutputMode("Header", "http://www.rjlg.com/XmlMashup#Header");
    public static final ExecutionOutputMode COOKIE
            = new ExecutionOutputMode("Cookie", "http://www.rjlg.com/XmlMashup#Cookie");
    public static final ExecutionOutputMode STATUSCODE
            = new ExecutionOutputMode("StatusCode", "http://www.rjlg.com/XmlMashup#StatusCode");
    public static final ExecutionOutputMode BODY
            = new ExecutionOutputMode("Body", "http://www.rjlg.com/XmlMashup#Body");
    public static final ExecutionOutputMode DEFAULT = BODY;

    public static final String MODE_DELIMITER = ":";
    private static final Pattern REGEX_PATTERN = Pattern.compile("([A-z]+)"+MODE_DELIMITER+"(.+)");
    private static final int REGEX_GROUP_MODE       = 1;
    private static final int REGEX_GROUP_MEMORYITEM = 2;


    private final String name;
    private final String namespace;

    private ExecutionOutputMode(String name, String namespace) {
        this.name = name;
        this.namespace = namespace;
        ExecutionOutputMode.allModes.put(name.toLowerCase(), this);
        ExecutionOutputMode.namespaces.add(namespace);
    }

    public String getName() {
        return name;
    }

    public String getNamespace() {
        return namespace;
    }

    public boolean isDefault() {
        return name.equalsIgnoreCase(DEFAULT.getName());
    }

    public static boolean isOutputModeNamespace(String str) {
        if ( str == null || str.isEmpty() ) {
            return false;
        }

        return ExecutionOutputMode.namespaces.contains(str);
    }

    public static ExecutionOutputMode getOutputModeForNamespace(String namespace) {
        if ( namespace == null || namespace.isEmpty() ) {
            return null;
        }

        for ( ExecutionOutputMode mode : allModes.values() ) {
            if ( mode.getNamespace().equals(namespace) ) {
                return mode;
            }
        }

        return null;
    }

    public static ExecutionOutputMode parseOutputMode(String str) {
        if ( str == null || str.isEmpty() ) {
            return DEFAULT;
        }

        Matcher matcher = REGEX_PATTERN.matcher(str);
        if ( !matcher.matches() ) {
            return DEFAULT;
        }

        String name = matcher.group(REGEX_GROUP_MODE);
        return allModes.get( name.toLowerCase() );
    }

    public static String parseMemoryItem(String str) {
        if ( str == null || str.isEmpty() ) {
            return str;
        }

        Matcher matcher = REGEX_PATTERN.matcher(str);
        if ( !matcher.matches() ) {
            return str;
        }

        return matcher.group(REGEX_GROUP_MEMORYITEM);
    }
}
